﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
using System.Reflection;
using System.Resources;

namespace DISDisplay
{
    class ClockDigit : Panel
    {
        System.Reflection.Assembly myAssembly;
        ResourceManager myManager;
        PictureBox pb1=new PictureBox();
        PictureBox pb2=new PictureBox();
        public bool locked = false;
        System.Windows.Forms.Timer tm1 = new System.Windows.Forms.Timer();
        int counter = 0;
        public char Symbol;
        public bool start = false;
        public int simb = 1;

        public ClockDigit(int width, int height)
        {
            myAssembly = this.GetType().Assembly;
            myManager = new System.Resources.ResourceManager("DISDisplay.Properties.Resources",myAssembly);
            this.Width = width;
            this.Height = height;
            this.BackColor = System.Drawing.Color.Green; //.IndianRed;
            this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            pb1.SetBounds(-2, (-1) * this.Height - 2, this.Width, this.Height);
            pb1.SizeMode = PictureBoxSizeMode.StretchImage;
            pb1.Image = (Bitmap)myManager.GetObject("tarpas");//new Bitmap(Image.FromFile(Application.StartupPath + "\\img\\tarpas.png"));
            pb2.SetBounds(-2, -2, this.Width, this.Height);
            pb2.SizeMode = PictureBoxSizeMode.StretchImage;
            pb2.Image = (Bitmap)myManager.GetObject("tarpas"); //new Bitmap(Image.FromFile(Application.StartupPath + "\\img\\tarpas.png"));
            tm1.Tick += new EventHandler(tm1_Tick);
            this.Controls.Add(pb1);
            this.Controls.Add(pb2);
           // this.OnResize+=new System.Windows.Forms.
        }

        void tm1_Tick(object sender, EventArgs e)
        {
            if (locked == true)
            {
                counter++;
                pb1.Top = pb1.Top + 10;
                pb2.Top = pb2.Top + 10;
                //if (counter <= Math.Round((double)this.Height/10))
                if (counter == 4)
                {
                    locked = false;
                    tm1.Enabled = false;
                    pb1.SetBounds(-2, (-1) * this.Height - 2, this.Width, this.Height);
                    pb2.SetBounds(-2, -2, this.Width, this.Height);
                    //pb1.Image = new Bitmap(Image.FromFile(Application.StartupPath + "\\img\\tarpas.png"));
                    if (Symbol == ' ')
                    {
                        pb1.Image = (Bitmap)myManager.GetObject("tarpas"); //new Bitmap(Image.FromFile(Application.StartupPath + "\\img\\tarpas.png"));
                    }
                    else
                    {
                        pb1.Image = (Bitmap)myManager.GetObject("_" + Symbol); //new Bitmap(Image.FromFile(Application.StartupPath + "\\img\\" + Symbol + ".png"));
                    }
                    
                    if (Symbol == ' ')
                    {
                        pb2.Image = (Bitmap)myManager.GetObject("tarpas"); //new Bitmap(Image.FromFile(Application.StartupPath + "\\img\\tarpas.png"));
                    }
                    else
                    {
                        pb2.Image = (Bitmap)myManager.GetObject("_" + Symbol); //new Bitmap(Image.FromFile(Application.StartupPath + "\\img\\" + Symbol + ".png"));
                    }
                    counter = 0;
                    if (start == true)
                    {
                        Start();
                    }
                }
            }
        }

        public void parseSymbol(char ch)
        {
            if (locked == false)
            {
                locked = true;
                if (ch == ' ')
                {
                    pb1.Image = (Bitmap)myManager.GetObject("tarpas"); //new Bitmap(Image.FromFile(Application.StartupPath + "\\img\\tarpas.png"));
                }
                else
                {
                    pb1.Image = (Bitmap)myManager.GetObject("_" + ch); //new Bitmap(Image.FromFile(Application.StartupPath + "\\img\\" + ch + ".png"));
                }
                tm1.Interval = 20;
                Symbol=ch;
                tm1.Enabled = true;
            }
        }

        public void Start()
        {
            start = true;
            string charPool = "";
            if (simb == 1)
            {
                charPool = "ABCDEFGHIJKLMNOPQRSTUVWXYĄČĘĖĮŠŲŪŽ";
            }
            else { charPool = "0123456789"; }
           // string charPool = "ABCDEFGHIJKLMNOPQRSTUVWXYĄČĘĖĮŠŲŪŽ";
            Random rnd = new Random();
            char ch=charPool[(int)(rnd.NextDouble() * charPool.Length)];
            parseSymbol(ch);
        }
    }
}
